package org.example.redisdemo.configuration;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.connection.stream.StreamInfo;
import org.springframework.data.redis.connection.stream.StreamRecords;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StreamOperations;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicBoolean;

@Component
public class RedisGroup {
    @Resource
    private RedisTemplate redisTemplate;

    private final AtomicBoolean isCreated = new AtomicBoolean(false);



//    @PostConstruct
//    public void groupInfo() {
//
//        // 发送个心跳,保证stream已经存在
//        HashMap<Object, Object> map = new HashMap<>();
//        map.put("fileBeat","fileBeat...");
//        final MapRecord<String, Object, Object> record = StreamRecords.newRecord()
//                .in("teststream")
//                .ofMap(map)
//                .withId(RecordId.autoGenerate());
//
//        final StreamOperations<String, Object, Object> stream = redisTemplate.opsForStream();
//        stream.add(record);
//
//        final StreamInfo.XInfoGroups xInfoGroups = stream.groups("teststream");
//
//        Collection<StreamInfo.XInfoGroup> needDestroyColl = new ArrayList<>();
//
//        xInfoGroups.forEach(xInfoStream -> {
//            if (xInfoStream.groupName().equals("groupA")) {
//                isCreated.set(true);
//            } else {
//                needDestroyColl.add(xInfoStream);
//            }
//        });
//
//        for (StreamInfo.XInfoGroup xInfoGroup : needDestroyColl) {
//            stream.destroyGroup("teststream",xInfoGroup.groupName());
//        }
//
//        if (isCreated.get()) return;
//
//        stream.createGroup("teststream", "groupA");
//
//    }
}
